<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="form.status" clearable class="filter-item" placeholder="状态">
        <el-option v-for="item in statusList" :key="item.value" :label="item.text" :value="item.value" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">
        查询
      </el-button>
    </div>

    <el-table
      v-show="list"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="序号" width="50px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="配置编码" min-width="160px">
        <template slot-scope="{row}">
          <span class="link-type" @click="edit(row.id)">{{ row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="配置名称" min-width="160px">
        <template slot-scope="{row}">
          <span class="link-type" @click="edit(row.id)">{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.lastUpdateDate }}</span>
        </template>
      </el-table-column>

      <el-table-column label="当前状态" width="80px" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.status | tagFilter">
            {{ row.status | statusFilter }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <div>
            <el-button
              size="mini"
              @click="edit(row.id)"
            >
              编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="postStatus(row, 2)"
            >
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getListData" />

  </div>
</template>

<script>
import { getTabelList, postStatus, upDateRow } from '@/api/contentConfig/index'
import Pagination from '@/components/Pagination'

const statusList = [
  { value: 'up', text: '上架', tag: 'success' },
  { value: 'down', text: '下架', tag: 'danger' },
  { value: 'draft', text: '草稿', tag: 'info' }
]

const statusValue = statusList.reduce((acc, cur) => {
  acc[cur.value] = cur.text
  return acc
}, {})

const statusTag = statusList.reduce((acc, cur) => {
  acc[cur.value] = cur.tag
  return acc
}, {})

export default {
  components: { Pagination },
  filters: {
    statusFilter(status) {
      return statusValue[status]
    },
    tagFilter(status) {
      return statusTag[status]
    }
  },
  props: {
    refresh: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      statusList,

      form: {
        status: 'up' // 当前状态
      },
      list: null,
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  watch: {
    // 监听是否需要重新刷新列表
    refresh(val) {
      if (val) {
        this.search()
      }
    }
  },
  created() {
    this.search()
  },
  methods: {
    /**
     * 新增tab页
     */
    edit(id) {
      this.$emit('editInfo', id)
    },
    // 查询
    search() {
      this.listQuery.page = 1
      this.getListData()
    },

    /**
     * 获取table数据
     * @param queryParam 查询数据参数名
     */
    getListData() {
      this.listLoading = true
      getTabelList(Object.assign({
        page: this.listQuery.page - 1,
        size: this.listQuery.limit
      }, this.form)).then(response => {
        this.list = response.data.content
        console.log(this.list)
        this.total = response.data.totalElements
        setTimeout(() => {
          this.listLoading = false
        }, 800)
      })
    },
    upDateRow(id, opera) {
      upDateRow({
        id,
        opera
      }).then(() => {
        this.getListData()
      })
    },

    // 上下架 0 上架，1 下架 2 删除
    postStatus(row, type) {
      const id = row.id
      const arr = ['上架', '下架', '删除']
      const arrMethod = ['put', 'delete', 'delete']
      this.$confirm(
        `确认${arr[type]}“${row.classifyName}”吗？`,
        '提示',
        { confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning' })
        .then(() => {
          postStatus({ id }, arrMethod[type]).then(re => {
            this.$notify({
              title: '成功',
              message: '操作成功',
              type: 'success',
              duration: 2000
            })
            this.getListData()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    }
  }
}
</script>
